package com.controller;

import java.util.ArrayList;
import java.util.Collection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.entity.MyTable;
import com.entity.TableInfo;
import com.entity.User;
import com.entity.ziduan;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.CreateTableService;
import com.service.impl.GetTableImpl;
import com.utils.tableToid;

@Controller

//这个控制器主要用于对用户创建表，插入，查看表的操作
public class CTableController {
	
	
	@Autowired
	private CreateTableService ctableservice;
	
	@Autowired
	private GetTableImpl gettable;
	
	private tableToid utils;
	

	
	
	//插入新表
	@RequestMapping("/ctable.action")
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,readOnly=false)
	public String ctable(@RequestBody MyTable jsons,String username,HttpSession session) {
		System.out.println(jsons.getTablename());
		ArrayList<ziduan> temp=jsons.getZiduans();
		
		//调试查看
		for(ziduan z:temp) {
			System.out.println("字段名："+z.getZiduanname()+"字段类型"+z.getZiduantype()+"默认为空？"+z.getNotn());
		}
		
		
		//这是一个事务由Spring保证其原子性与数据一致性
		//调用service层创建表
		//生成一个全局唯一的表名
	    tableToid cid=new tableToid();
	    String nid=cid.generateid(jsons.getTablename());
	    User tempu=(User)session.getAttribute("Users");
		System.out.println("生成的表名"+nid);
		String tablename=jsons.getTablename();
		jsons.setTablename(nid);
		ctableservice.createTable(jsons);
		System.out.println("用户id"+tempu.getId());
		System.out.println("自定义表明："+tablename);
		int uid=tempu.getId();
		ctableservice.insertNewTable(uid,tablename,nid);//将新表插入表信息表
    	return "redirect:./list.action";
	}
	
	
	
	
	
	//获取用户表,采用mybatis pagehelp分页插件
	@RequestMapping(value="/list.action")
	public String list(@RequestParam(required=true,defaultValue="1") int page,Model model,HttpSession session) {
    	System.out.println("进来了");
    	PageHelper.startPage(page,3);
    	User tempu=(User) session.getAttribute("Users");
    	int uid=tempu.getId();
		List<TableInfo> mytables=ctableservice.findMyTable(uid);
		//创建PageInfo对象，保存查询出的结果，PageInfo是pageHelper中的对象
		PageInfo<String> uss=new PageInfo(mytables);
		
    	model.addAttribute("mytables",mytables);
    	model.addAttribute("page",uss);
    	
		return "hello";
	}
	
	//查看指定表的数据
	@RequestMapping(value="/showtable.action")
	public String showtable(@RequestParam(value="tablename")String tablename,Model model) {
		System.out.println("查找表明"+tablename);
		List<Map> temp1=gettable.getTableContent(tablename);
		List<ziduan> temp2=gettable.getZiduan(tablename);
		for(int i=0;i<temp1.size();i++) {
			//List filedsInfo = utils.getFiledsInfo(temp1.get(i));
			System.out.println(temp1.get(i).toString());
		}
		
		model.addAttribute("tablename",tablename);
		model.addAttribute("tcontent",temp1);
		model.addAttribute("ziduans", temp2);
		//return null;
		return "tablecontent";
	}
	
	//用于从查看表中数据跳转到对当前表进行插入操作
	@RequestMapping(value="/inserttable.action")
	public String inserttable(@RequestParam(value="tablename")String tablename,Model model) {
		
		List<ziduan> temp2=gettable.getZiduan(tablename);
		model.addAttribute("tablename",tablename);
		model.addAttribute("ziduans", temp2);
		return "insertdata";
		
		
		
	}
	
	/*向指定数据表插入一行数据
	 * 
	 * 前端获取的字段数据依据key-value插入到map中
	 * 
	 */
	@RequestMapping(value="/insertrow.action")
	public String insertrow(@RequestParam(value="tablename")String tablename,@RequestParam Map<String,String> mapz) {
		
		System.out.println(tablename);
	
			
		//值 map转list，将map中包含的表名tablename去除掉，方便后续动态SQL插入操作
		mapz.keySet().removeIf(key -> key =="tablename");
		mapz.values().removeIf(value -> value.contains(tablename));
		Collection<String> valueCollection=mapz.values();
		List<String> values=new ArrayList<String>(valueCollection);
		for(String i:values) {
			System.out.println(i);
		}
		
		//将ziduan类抽出其中的字段名封装成list方便后续操作
		List<ziduan> temp1=gettable.getZiduan(tablename);
		List<String> ziduans=new ArrayList<>();
		for(ziduan tempz:temp1) {
			ziduans.add(tempz.getZiduanname());
		}
		//插入
		ctableservice.insertNewRow(tablename, ziduans, values);
		
		return "redirect:./showtable.action?tablename="+tablename;
	}
	
	
	
}
